package com.minelx.landisk.filesystem.core.values;

import java.util.Objects;

public final class SchemeAndAuthority {
	private final String scheme;

	private final String authority;

	private SchemeAndAuthority(String scheme, String authority) {
		this.scheme = scheme;
		this.authority = authority;
	}

	public String toText() {
		// FIXME wait for:  convert path to empty string
		if (authority() == null) {
			return scheme() + "://";
		}
		return scheme() + "://" + authority();
	}

	public String scheme() {
		return scheme;
	}

	public String authority() {
		return authority;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) return true;
		if (obj == null || obj.getClass() != this.getClass()) return false;
		SchemeAndAuthority that = (SchemeAndAuthority) obj;
		return Objects.equals(this.scheme, that.scheme) &&
				Objects.equals(this.authority, that.authority);
	}

	@Override
	public int hashCode() {
		return Objects.hash(scheme, authority);
	}

	@Override
	public String toString() {
		return "SchemeAndAuthority{" +
				"scheme='" + scheme + '\'' +
				", authority='" + authority + '\'' +
				'}';
	}

	public static SchemeAndAuthority of(String scheme, String authority) {
		return new SchemeAndAuthority(scheme, authority);
	}

	public static SchemeAndAuthority from(String pattern) {
		String[] segments = pattern.split("://");
		if (segments.length == 1) {
			return SchemeAndAuthority.of(segments[0], "");
		}
		return SchemeAndAuthority.of(segments[0], segments[1]);
	}
}
